<?php
/**
 * Created by PhpStorm.
 * User: bzyy
 * Date: 2015-8-20 16:55:59
 * 短信发送功能
 *
 *
 *
 */
class SmsModel extends BaseModel {


    protected $url = 'http://sdk.entinfo.cn:8061/mdsmssend.ashx';
    protected $name = 'SDK-BBX-010-23073';
    protected $pwd;
    protected $file = 'sms_log.txt';

    function __construct($params, $db)
    {
        parent::__construct($params, $db);
        $this->pwd = strtoupper(md5('SDK-BBX-010-23073'.'5)))e)a3'));
        $this->file = 'sms_log_'.date('m').'.txt';
    }

    public function sendMessage($mobile,$content){

        $ch = curl_init();
        $curlPost = http_build_query(array(
            'sn' => $this->name,
            'pwd' => $this->pwd,
            'mobile' => $mobile,
            'content' => $content,
        ));

        //$text = "手机号：{$mobile} 内容：{$content} 时间：".date('Y-m-d H:i:s')." 短信编号：{$content} \r\n";

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, count($curlPost));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        $content_id = curl_exec($ch);
        curl_close($ch);

        $text = "手机号：{$mobile} 内容：{$content} 时间：".date('Y-m-d H:i:s')." 短信编号：{$content_id} \r\n";

        $this->log($text);
        //echo $text;
    }

    public function log($text){
        $file = ROOT_PATH.'/temp/logs/'.$this->file;
        @file_put_contents($file,$text,FILE_APPEND);
    }

    public function sendtomember($mid,$content){
        $model = m('member');
        $info = $model ->get_info($mid);
        if(empty($info)){
            return false;
        }

        if(!preg_match('/^((\(\d{2,3}\))|(\d{3}\-))?(13|15|18)\d{9}$/', trim($info['phone_mob']))){
            return false;
        }

        $this->sendMessage($info['phone_mob'],$content);

    }







}